//Binary_conversion_helper.h
#ifndef Binary_conversion_helper_H
#define Binary_conversion_helper_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#ifdef __cplusplus  /*If compile C++*/
/*Let C code be defined in C++*/
extern "C" {
#endif


const char *D_to_B_original(int num);       //十进制转二进制原码
const char *D_to_B_inverse(int num);        //十进制转二进制反码
const char *D_to_B_complement(int num);     //十进制转二进制补码,建议参数为8,16,32,64位的机器位数



int B_complement_to_D(const char *str);     //二进制补码转十进制
int B_original_to_D(const char *str);       //二进制原码转十进制




void show_int_bytes(int x);                 //以十六进制形式打印变量的字节位码
void show_float_bytes(float x);
void show_pointer_bytes(void *x);





int uadd_ok(unsigned x,unsigned y);         //判断两个无符号整数相加是否溢出
int tadd_ok(int x,int y);                   //判断两个有符号整数相加是否溢出
int tmult_ok(int x,int y);                  //判断两个有符号整数相乘是否溢出




//将IEEE 754 浮点数表示转换为十进制
float float_IEEE_conversion_to_D(const char *f_IEE);    
double double_IEEE_conversion_to_D(const char *f_IEE);






#ifndef PRINT_BINARY
//打印变量的二进制补码结果,不支持浮点类型
#define PRINT_BINARY(x) \
    { \
        printf("The number of bits is %d , the value is %lld, and the binary is :\t",sizeof(x)*8, (long long)(x)); \
        for (int i = sizeof(x) * 8 - 1; i >= 0; i--) { \
            printf("%u", ((unsigned long long)x >> i) & 1 ); \
            if ((i % 4 == 0) && (i != 0)) putchar(' '); \
        } \
        putchar('\n'); \
    }\

#endif


#ifndef PRINT_BINARY_FLOAT
//打印浮点数在内存中的位表示,利用联合体内存共享的特点打印位
/*
* IEEE 754 浮点表示标准 (sign significand exponent)
* sign = 0 表示正数，sign = 1 表示负数,
* significand M 是一个二进制小数,它的范围是1 \~ 2- epsilon ,或则 0 \~ 1- epsilon
* exponent E 的作用是对浮点数加权,这个权重是2的E次幂 (可能是负数)
* V = (-1)^s x M x 2^E
**/
#define PRINT_BINARY_FLOAT(x) \
    { \
        union { \
            float f; \
            unsigned int ui; \
        } u; \
        u.f = (x); \
        printf("The number of bits is %d , the value is %f, and the binary is :\t", sizeof(u.f) * 8, u.f); \
        for (int i = sizeof(u.f) * 8 - 1 , space_place = 0 ; i >= 0; i-- , space_place++) { \
            printf("%u", (u.ui >> i) & 1); \
            if (!space_place || (space_place % 4 == 0)) putchar(' '); \
            if ((i == 31) || (i == 23)) {\
            space_place=0; \
            putchar('\t'); \
            }\
        } \
        putchar('\n'); \
    }

#endif


#ifndef PRINT_BINARY_DOUBLE

#define PRINT_BINARY_DOUBLE(x) \
    { \
        union { \
            double d; \
            unsigned long long ull; \
        } u; \
        u.d = (x); \
        printf("The number of bits is %d , the value is %f, and the binary is :\t", sizeof(u.d) * 8, u.d); \
        for (int i = sizeof(u.d) * 8 - 1 , space_place = 0 ; i >= 0; i-- , space_place++) { \
            printf("%u", (u.ull >> i) & 1); \
            if (!space_place || (space_place % 4 == 0)) putchar(' '); \
            if ((i == 63) || (i == 52)) {\
            space_place=0; \
            putchar('\t'); \
            }\
        } \
        putchar('\n'); \
    }

#endif


#ifdef __cplusplus
}
#endif




#ifndef Binary_conversion_helper_C
#define Binary_conversion_helper_C
#include "Binary_conversion_helper.c"
#endif



#endif



